package com.pishou.code.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;


public class HttpPostClient {
	
	private static final String USERNAME = "24hroom";
	private static final String PASSWORD = "24hroom";

	public static boolean isConnect(String urlAddress) {
		boolean  b = false;
		try {
			URL url = new URL(urlAddress);
			InputStream in = url.openStream();
			b = true;
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String jsonstr="";
        try {
            URL realUrl = new URL(url);

            URLConnection conn = realUrl.openConnection();

            conn.setRequestProperty("accept", "application/json");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("username", USERNAME);
            conn.setRequestProperty("password", PASSWORD);
            conn.setDoOutput(true);
            conn.setDoInput(true);

            OutputStreamWriter outWriter = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out = new PrintWriter(outWriter);

            out.print(param);

            out.flush();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                jsonstr += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            } catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return jsonstr;
    }
    


}
